package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.TeachBuildingDto;
import com.ly.cloud.entity.TeachBuildingPo;
import com.ly.cloud.entity.XnPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.TeachBuildingMapper;
import com.ly.cloud.service.TeachBuildingService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.CampusBasicInfoVo;
import com.ly.cloud.vo.TeachBuildingTyVo;
import com.ly.cloud.vo.TeachBuildingVo;

/** 
 * Class Name: TeachBuildingServiceImpl  
 * Description: 
 * @author: luhuacong
 * @date: 2018年12月12日
 * @version: 1.0
 *
 */  
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class TeachBuildingServiceImpl implements TeachBuildingService {

	@Autowired
	private TeachBuildingMapper teachBuildingMapper;
	/** 
	 * Method Name:  insert
	 * Description:  
	 * 添加教学楼信息
	 * @param dto
	 * @param createMan 创建人
	 * @return boolean
	 * @author luhuacong
	 * @date: 2018年12月12日
	 */
	@Override
	public boolean insert(TeachBuildingDto dto,String createMan) {
		
		try {
			TeachBuildingPo po = new TeachBuildingPo();
			BeanUtils.copyProperties(dto, po);
			po.setCjr(createMan);
			po.setCjsj(new Date());
			int i = teachBuildingMapper.insert(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("添加教学楼失败！", e);
		}
	}
	
	/** 
	 * Method Name: deleteByid
	 * Description:  
	 * 删除教学楼
	 * @param id 
	 * @author LuHuaCong
	 * @date: 2018年12月13日
	 */
	@Override
	public boolean deleteByid(String id) {
		try {
			Integer i = teachBuildingMapper.deleteById(id);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("根据id删除教学楼信息失败!", e);
		}
	}

	/** 
	 * Method Name: update
	 * Description:  
	 * 修改教学楼信息
	 * @param dto
	 * @param modifyMan 修改人 
	 * @author LuHuaCong
	 * @date: 2018年12月13日
	 */
	@Override
	public boolean update(TeachBuildingDto dto,String modifyMan) {
		TeachBuildingPo po = new TeachBuildingPo();
		BeanUtils.copyProperties(dto, po);
		po.setXgr(modifyMan);
		po.setXgsj(new Date());
		try {
			int i = teachBuildingMapper.updateById(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("修改教学楼信息失败！", e);
		}
	}

	/** 
	 * Description:  根据校区编号和教学楼名称查询教学楼信息
	 * @param page 
	 * @param campusId 校区编号
	 * @param teachBuildName 教学楼名称
	 * @return List<TeachBuildingVo>
	 * @author LuHuaCong
	 * @date: 2018年12月13日
	 */
	@Override
	public Page<TeachBuildingVo> selectTeachBuildingList(Page<TeachBuildingVo> page, String campusId,
			String teachBuildName) {
		try {
			Page<TeachBuildingVo> records = page.setRecords(teachBuildingMapper.selectTeachBuildByCondition(page, campusId, teachBuildName));
			return records ;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/** 
	 * Description:  查询所有教学楼信息
	 * @param page 
	 * @return List<TeachBuildingVo>
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public Page<TeachBuildingVo> selectAllTb(Page<TeachBuildingVo> page) {
		try {	
			Page<TeachBuildingVo> records = page.setRecords(teachBuildingMapper.selectAll(page));
			return records ;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("教学楼信息就显示异常，请重试!", e);
		}
	}

	/** 
	 * Description:  查询教学楼编号和名称，用于下拉列表
	 * @param xqdm 校区代码
	 * @return List<TeachBuildingVo>
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public List<TeachBuildingTyVo> selectTeachBuildTy(String xqdm) {
		List<TeachBuildingPo> list = null;
		try {
			if(xqdm==null){
				list = teachBuildingMapper.selectList(null);
			}else {
				//调用BaseMapper中的带参数查询
				EntityWrapper<TeachBuildingPo> ew = new EntityWrapper<>();
				ew.where("XQH={0}", xqdm);
				list = teachBuildingMapper.selectList(ew);
			}
			return BeanCopyUtils.copyListProperties(list, TeachBuildingTyVo.class);
		} catch (Exception e) {
			throw new CloudException("获取教学楼信息失败，请重试!", e);
		}
	}

}
